package com.example.rbacmanage.controller.background;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.Pv;
import com.example.rbacmanage.entity.app.Uv;
import com.example.rbacmanage.service.app.UvService;
import com.example.rbacmanage.util.DateUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


@RestController
@RequestMapping("/background/uv")
public class UvController{
    @Autowired
    private UvService uvService;

    @GetMapping("/list")
    public Result page(Uv uv){
        return Result.success(uvService.getPage(uv));
    }

    @GetMapping("/alllist")
    public Result alllist(Uv uv){
        Map<String,Object> map = new HashMap<>();
        map.put("beginTime", DateUtils.getStartDate(DateUtils.getBeforeDate(7)));
        map.put("endTime",DateUtils.getEndDate(DateUtils.getStartDate()));
        uv.setParams(map);
        //获取同一天的数据量
        Map<String,Integer> map1 = new HashMap<>();
        List<Uv> list = uvService.getAll(uv);
        for (Uv uv1 : list) {
            String date = com.alibaba.fastjson2.util.DateUtils.format(uv1.getCreateTime(),"yyyy-MM-dd");
            if (map1.containsKey(date)) {
                map1.put(date,map1.get(date)+1);
            }else{
                map1.put(date,0);
            }
        }
        List<String> dates = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i <= 7; i++) {
            String date = currentDate.minusDays(i).format(formatter);
            dates.add(date);
        }
        Collections.reverse(dates);
        //获取存在数量的日期
        List<String> keys=new ArrayList<>();
        map1.forEach((k,v)->{
            keys.add(k);
        });
        List<Integer> list1 = new ArrayList<>();
        dates.forEach(date->{
            if (keys.contains(date)){
                list1.add(map1.get(date));
            }else{
                list1.add(0);
            }
        });
        return Result.success(list1);
    }
}
